<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="$store.state.rout.url.a">{{$store.state.rout.url.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <!-- <XtxCarousel :imgList="sliders" style="height:500px" /> -->
      <lbt :width="'1240'" :height=" '400'" @add="slidersFn" :imgList="sliders"/>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="itme in goods.children" :key="itme.id">

            <a @click="data" href='#'>
              <img :src="itme.picture" >
              <p>{{itme.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
        <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="(obj,index) in goods.children" :key="index">
        <div class="head" >
          <h3>- {{obj.name}} -</h3>
          <p class="tag">{{obj.desc}}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem  :goods='obj.goods'  />
        </div>
      </div>

    </div>
  </div>
</template>
<script>
import GoodsItem from './components/goods-item.vue'
import { computed, ref, watch, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { findBanner, findTopCategory } from '@/api/home'
import { useStore } from 'vuex'
export default {
  name: 'TopCategory',
  components: {
    GoodsItem
  },
  setup () {
    let ace = true
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    console.log(store.state.calt.list)
    const arr = computed(() => {
      const calt = store.state.calt.list.find(item =>
        item.id === route.params.id

      )
      return calt
    })
    const data = () => {
      console.log(route)
      router.push('/category/DetailData:id')
    }
    console.log(route)
    const goods = ref([])
    // 监听路由参数的变化
    watch(() => route.params.id, (newVal) => {
      // 获取相关商品的数据
      if (ace) {
        findTopCategory(newVal).then(ret => {
          goods.value = ret.result
          console.log(goods.value, 'hhhh')
        })
      }
    }, {
      immediate: true
    })
    onMounted(() => {
      ace = false
    })

    // 轮播图
    const sliders = ref([])
    findBanner().then(data => {
      sliders.value = data.result
    })
    const slidersFn = () => {
      // sliders.value.push(sliders.value[0])
      // sliders.value.shift()
    }

    return { sliders, arr, goods, slidersFn, data }
  }
}
</script>
<style scoped lang="less">
.body {
    display: flex;
}
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>
